package com.ruoyi.wx.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.PermissionService;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.wx.entity.PtScore;
import com.ruoyi.wx.entity.PtVideo;
import com.ruoyi.wx.service.IPtScoreService;
import com.ruoyi.wx.service.IPtVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 视频表 前端控制器
 * </p>
 *
 * @author moxiu
 * @since 2022-03-04
 */
@RestController
@Api(value = "视频相关接口", tags = {"视频相关接口测试"})
@RequestMapping("/wx")
public class PtVideoController extends BaseController {

    @Autowired
    private IPtVideoService ptExpVideoService;

    @Autowired
    private IPtScoreService ptScoreService;

    @Autowired
    private PermissionService permissionService;

    @ApiOperation("查看该实验下有哪些视频")
    @GetMapping("/getVideo")
    public AjaxResult getVideo(Long expId) {
        return AjaxResult.success(ptExpVideoService.list(Wrappers.lambdaQuery(PtVideo.class)
            .eq(PtVideo::getExpId, expId)));
    }

    @ApiOperation("记录用户看了这个视频")
    @GetMapping("/recordWatch")
    public AjaxResult recordWatch(Long videoId, Long expId) {
        return toAjax(ptExpVideoService.recordWatch(videoId, expId));
    }

    @ApiOperation("查询用户看了什么视频")
    @GetMapping("/allWatch")
    public AjaxResult allWatch(Long expId) {
        PtScore one = ptScoreService.getOne(Wrappers.lambdaQuery(PtScore.class)
            .eq(PtScore::getExpId, expId).eq(PtScore::getUserId, SecurityUtils.getUserId())
            .select(PtScore::getVideoList)
        );
        if (one == null) {
            return AjaxResult.success();
        }
        String videoList = one.getVideoList();

        return AjaxResult.success((List<Integer>) JSONObject.parse(videoList));
    }

    @ApiOperation("给实验上传视频")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "文件", dataTypeClass = File.class, required = true),
    })
    @PostMapping("/uploadVideo")
    public AjaxResult uploadVideo(@RequestPart("file") MultipartFile file, Long expId) {
        if (!permissionService.hasTeacherOrAdmin()) {
            return AjaxResult.error("没有权限");
        }

        if (file == null) {
            return AjaxResult.error("实验报告不能为空");
        } else {
            String originalfileName = file.getOriginalFilename();
            String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
            if (!".mp4".equals(suffix)) {
                return AjaxResult.error("mp4");
            }
            ptExpVideoService.uploadVideo(file, expId);
            return AjaxResult.success();
        }
    }
}
